<?php

require_once("functions.php");
require_once("db_functions.php");
require_once("page.class.php");

define("PAGE_LENGTH",50);

class logs extends page{
	
	var $db;
	var $states = array(
		1 => "novi",
		2 => "izmena",
		3 => "brisanje"
	);
	
	var $NETBIOS;
	
	function logs(){
		
		$this->db = new db_functions();
		
		if (!$this->isUserLoggedIn()) {
			echo $this->parseTemplate("login.tpl");
		}
		
		$a = getFromInput("a");
		
		switch ($a){
			case "netbios":
				$ip = getFromInput("ip");
				$out = '<div class="area">';
				$out .= "<p><strong>IP adresa:</strong>";
				$out .= "<br />$ip</p>";
				$out .= "<p><strong>Naziv računara:</strong>";
				$out .= "<br />".$this->getNetBIOSName($ip)."</p>";
				$out .= '</div>';
				echo $out;
				break;
			default:
				$this->browse();
				break;
		}
	}
	
	function browse(){
		
		$page = getFromInput("page");
		$page = $page>0?$page:1;
		$start = ($page-1)*PAGE_LENGTH;
		
		$fUser = getFromInput("filter_user");
		$polja = $this->get_Users($fUser);
			$this->assign("user_id",$polja["polje_id"]);
			$this->assign("user",$polja["polje_user"]);
			$this->assign("user_ime",$polja["polje_ime"]);
			$this->assign("user_prezime",$polja["polje_prezime"]);
			$this->assign("user_selected",$polja["polje_selected"]);
			
		$fOd = getFromInput("filter_od");
		$fTOd = date2int($fOd);
		$this->assign("filter_od",$fTOd>0?date("d.m.Y",$fTOd):"");
		
		$fDo = getFromInput("filter_do");
		$fTDo = date2int($fDo);
		$this->assign("filter_do",$fTDo>0?date("d.m.Y",$fTDo):"");
		
		$fState = getFromInput("filter_state");
		$polja = $this->get_LogStates($fState);
			$this->assign("state_id",$polja["polje_id"]);
			$this->assign("state_name",$polja["polje_ime"]);
			$this->assign("state_selected",$polja["polje_selected"]);
			
		$fTable = readValFromInput("filter_table");
		$res = $this->db->select2("SELECT DISTINCT `table` FROM log ORDER BY `table`");
		$tables = array();
		if ($res !== false) {
			foreach ($res as $k=>$v){
				$tables[$k] = $v["table"];
				$tables_selected[$k] = $v["table"]==$fTable?" selected":"";
			}
		}
		$this->assign("table_name",$tables);
		$this->assign("table_selected",$tables_selected);
		
		$fValue = readValFromInput("filter_value");
		$this->assign("filter_value",$fValue);
		
		$q_select = "SELECT *";
		$q_from = "FROM log";
		$q_where = "WHERE 1";
		
		if ($fUser>0) {
			$q_where .= " AND user=$fUser";
		}
		if ($fTOd>0) {
			$q_where .= " AND time>=$fTOd";
		}
		if ($fTDo>0 && $fTDo>$fTOd) {
			$q_where .= " AND time<=".($fTDo+(60*60*24));
		}
		if ($fState>0) {
			$q_where .= " AND state=$fState";
		}
		if (strlen($fTable)>0) {
			$q_where .= " AND `table`='$fTable'";
		}
		if (strlen($fValue)>0) {
			$q_where .= " AND `value` LIKE '%$fValue%'";
		}
		
		$q_order = "ORDER BY time DESC "; 
		
		$q_limit = "LIMIT $start,".PAGE_LENGTH;
		
		$resall = intval($this->db->getOne("SELECT count(*) $q_from $q_where"));
		$q = "$q_select $q_from $q_where $q_order $q_limit";
		$res = $this->db->select2($q);
		//echo "$q";
		if ($res !== false) {
			
			$numpages = bcdiv($resall,PAGE_LENGTH,0);
			if ($resall%PAGE_LENGTH) {
				$numpages++;
			}
			$np = 10;
			$sp = $page>$np?$page>$numpages-$np?$page-(2*$np):$page-$np:1;
			$ep = $page>$numpages-$np?$numpages:$sp+21;
					
			for ($i=$sp; $i<=$ep; $i++){
				$p[] = $i;
				if ($i==$page) {
					$cp[] = "current";
				}else{
					$cp[] = "";
				}
			}
			$this->assign("numpages",$numpages);
			$this->assign("page",$p);
			$this->assign("current_page",$cp);
			
			$this->assign("info","Ukupno: $resall");
			
			foreach ($res as $k=>$row){
				$user_id	= $row["user"];
				$time		= $row["time"];
				$ip			= $row["ip"];
				$state		= $row["state"];
				
				list($username, $radnik_ime,$radnik_prezime) = $this->db->getRow("SELECT u.username, radnik_ime, radnik_prezime FROM users u, radnici r WHERE u.radnik_id=r.radnik_id AND u.id=$user_id");
				
				$actions[] = $k;
				$usernames[] = $username;
				$firsts[] = $radnik_ime;
				$lasts[] = $radnik_prezime;
				
				$ips[] = "$ip";
				
				$times[] = date("d.m.Y H:i",$time);
				$table[] = $row["table"];
				$value[] = $row["value"];
				$act[] = $this->states[$state];
			}
			$this->assign("action",$actions);
			$this->assign("time",$times);
			$this->assign("username",$usernames);
			$this->assign("radnik_ime",$firsts);
			$this->assign("radnik_prezime",$lasts);
			$this->assign("ip",$ips);
			$this->assign("table",$table);
			$this->assign("value",$value);
			$this->assign("akcija",$act);
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->Nema rezultata");
		}
		
		echo $this->parseTemplate("logs/browse.tpl");
	}
	
	function getNetBIOSName($ip){
		$command = "nmblookup -A $ip";
		if (!isset($this->NETBIOS[$ip])) {
			exec($command,$out);
			$line = $out[1];
			if (preg_match('/^\t/',$line)) {
				$name = preg_split('/\t(.*?)\s+/',$line,-1,PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
				$this->NETBIOS[$ip] = $name[0];
			}else{
				$this->NETBIOS[$ip] = "unknown";
			}
		}
		return $this->NETBIOS[$ip];
	}
	
	//------------------------------------------------------------
	/*		GET FRUNCTIONS		*/
	
	function get_Users($userid=null){
		$ret = array();
		$q = "SELECT id, username, radnik_ime, radnik_prezime "
			."FROM users u, radnici r "
			."WHERE u.radnik_id=r.radnik_id "
			."ORDER BY radnik_ime, radnik_prezime, username";
		$res = $this->db->select2($q);
		if ($res !== false) {
			foreach ($res as $k=>$v){
				$ret["polje_id"][$k] = $v["id"];
				$ret["polje_user"][$k] = $v["username"];
				$ret["polje_ime"][$k] = $v["radnik_ime"];
				$ret["polje_prezime"][$k] = $v["radnik_prezime"];
				if ($userid == $v["id"]) {
					$ret["polje_selected"][$k] = " selected";
				}else{
					$ret["polje_selected"][$k] = "";
				}
			}
		}
		
		return $ret;
	}
	
	function get_LogStates($stateid){
		$ret = array();
		foreach ($this->states as $k=>$v){
			$key = count($ret["polje_id"]);
			$ret["polje_id"][$key] = $k;
			$ret["polje_ime"][$key] = $v;
			if ($stateid == $k) {
				$ret["polje_selected"][$key] = " selected";
			}else{
				$ret["polje_selected"][$key] = "";
			}
		}
		
		return $ret;
	}
}

?>